$(function () {
    var accident_datagrid = $("#accident_datagrid");
    var client_datagrid = $("#client_datagrid");
    var accident_dialog = $("#accident_dialog");
    var accident_form = $("#accident_form");

    accident_datagrid.datagrid({
        fit: true,
        url: "/accident/list",
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        toolbar: "#tb",
        singleSelect: true,
        columns: [[
            {field: "ck", checkbox: true}, //显示多选框样式
            {field: "id", title: "编号", width: 100, hidden: true},
            {
                field: "client", title: "报案人姓名", width: 100,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },

            //不能只局限于用value取值,当出现冲突的时候要用到(row)
            //比如在这里:从一个对象中获取多个属性的时候,fileld对应的是类中的字段,不可能出现多个相同,因此用到
            //row.对象名.属性(意思也就是从这一行中的某个对象中,获取到他的属性)
            {
                field: "gender", title: "性别", width: 100, formatter: function (value, row, index) {
                if (row.client) {
                    if (row.client.gender == 0) {
                        return "女";
                    } else {
                        return "男";
                    }
                } else {
                    return "";
                }
            }
            },
            {
                field: "tel", title: "联系方式", width: 100, formatter: function (value, row, index) {
                if (row.client) {
                    return row.client.tel;
                } else {
                    return "";
                }
            }
            },
            {
                field: "email", title: "邮箱", width: 100, formatter: function (value, row, index) {
                if (row.client) {
                    return row.client.email;
                } else {
                    return "";
                }
            }
            },
            {field: "oTime", title: "发生时间", width: 100, sortable: true},

            {field: "oLocation", title: "发生地点", width: 100},

            {
                field: "guaranteeBill", title: "保险单号", width: 100, formatter: function (value, row, index) {
                    if(value){
                        return value.number;
                    }
                }
            },
            {
                field: "inputUser", title: "录入人", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "";
                }
            }
            },

            {
                field: "status", title: "当前状态", width: 100, formatter: function (value, row, index) {

                    if (value == 0) {
                        return "<font color='blue'>待验证</font>";
                    } else if (value == 1) {
                        return "<font color='#a52a2a'>已验证，待赔付</font>"; //理赔明细
                    }
                    else if (value == 2) {
                        return "<font color='#00008b'>废弃</font>";
                    } else if (value == 3) {
                        return "<font color='#00ced1'>待审核</font>"; //审核单
                    } else if (value == 4) {
                        return "<font color='#6495ed'>被拒审核</font>";
                    } else if (value == 5) {
                        return "<font color='#daa520'>审核完,待付款给保人</font>"; //理赔单
                    } else if (value == 6) {
                        return "<font color='#ff6347'>已赔付</font>";
                    }
            }
            }
        ]],
        //当状态为"待验证"时,具有编辑权限;
        //当状态为"已验证"时,具有查看权限;
        onClickRow: function (index, row) {
            //console.log(row);
            //不是待验证状态 全是查看
            if (row.status == 0) {
                $("#edit").linkbutton({
                    text: "编辑"
                })
            } else {
                $("#edit").linkbutton({
                    text: "查看"
                })
            }
        }
    });

    accident_dialog.dialog({
        width: 400,
        height: 350,
        closed: true,
        buttons: "#bb"
    });


    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            accident_datagrid.datagrid("reload");
        },

        //工具栏添加
        add: function () {
            //添加与编辑时
            accident_form.form("clear");
            //1.弹框
            accident_dialog.dialog("open");
            //设置标题
            accident_dialog.dialog("setTitle", "事故添加","seccess");
        },
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = accident_datagrid.datagrid("getSelected");
            if (row.status == 1) {
                $("#btn_save").linkbutton({disabled: true});
            } else if (row.status == 0) {
                $("#btn_save").linkbutton({disabled: false});
            }
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }

            //添加与编辑时将数据清空
            accident_form.form("clear");
            //解决编辑回显时,row中没有client这一行的问题
            if (row.client) {
                row['client.id'] = row.client.id;
            }

            //数据回显: 同名原则
            accident_form.form("load", row);
            //1.弹框
            accident_dialog.dialog("open");
            //设置标题
            accident_dialog.dialog("setTitle", "事故编辑");
        },


        //保存操作
        save: function () {
            var id = $("#accidentId").val();
            var url = "/accident/save";
            if (id) {
                url = "/accident/update";
            }
            accident_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "添加成功", "seccess");
                        //关闭框
                        accident_dialog.dialog("close");
                        //刷新列表数据
                        accident_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //在表单提交的时候把投保单号带进去,交给服务端处理(参数?)
        onSubmit: function (param) {
            var row = client_datagrid.datagrid('getSelected');
            if (row == null) {
                return;
            }
            param.client_id = row.guaranteeBill.policyClient.id;

        },
        //关闭
        cancel: function () {
            accident_dialog.dialog("close");
        },

        //高级查询工具
        query: function () {
            //因为easyui对元素的html进行相关封装,可能导致获取不到数据,建议都使用easyui提供获取数据方法
            //var keyword = $("#keyword").val();
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");

            //所谓高级查询,无非是让datagrid根据条件去更新列表数据,所以只需要刷新列表,同时携带查询条件即可
            accident_datagrid.datagrid("load", {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //工具栏验证
        changeStatus: function () {
            //判断是否选中数据
            var row = accident_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert("温馨提示", "请选择要验证的数据");
                return;
            }
            $.messager.confirm('确认', '您确定要执行这个操作?', function (r) {
                if (r) {
                    //1.发起请求修改状态
                    $.get("/accident/changeStatus", {id: row.id}, function (data) {
                        if (data.success) {
                            accident_datagrid.datagrid("reload");
                            $.messager.alert("温馨提示", "验证成功", "success");
                        } else {
                            $.messager.alert("温馨提示", data.msg);
                        }
                    })
                }
            })
        }

    };

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    }),

        $("#client").combogrid({
            panelWidth: 425,
            idField: 'id',
            rownumbers: true,
            striped: true,
            fitColumns: true,
            textField: 'name',
            pagination: true,
            valueField: 'id',
            singleSelect: true,
            fit: true,
            url: '/clientfollowup/list',
            columns: [[
                {field: 'id', id: 'id', align: "center"},
                {field: 'name', title: '姓名', width: "100"},
                {
                    field: 'gender',
                    title: '性别',
                    width: "100",
                    allowSort: "true",
                    align: "center",
                    headerAlign: "center",
                    formatter: function (value, row, index) {
                        return value ? "男" : "女";
                    }
                },
                {field: 'tel', title: '电话', width: 100},
                {field: 'email', title: '邮箱', width: 100}
            ]]
        });
});


